#include "image.h"

#include <assert.h>

Image::Image( unsigned int bytesPerRow, unsigned char *data, SIZE size )
{
	// The number of bytes per row must be large enough to store the row.
	assert( bytesPerRow >= (unsigned int) size.cx * 3 );

	_bytesPerRow = bytesPerRow;
	_data = data;
	_size = size;
}

Image& Image::operator =( const Image &img )
{
	//Both images must have the same size.
	assert( _size.cx = img._size.cx );
	assert( _size.cy = img._size.cy );

	//Copy the bitmap data.
	_bytesPerRow = img._bytesPerRow;
	for( unsigned int row = 0; row < (unsigned int) abs( _size.cy ); ++row )
	{
		unsigned int offset = row * _bytesPerRow;
		memcpy( _data + offset, img._data + offset, _size.cx * 3 );
	}

	return *this;
}

unsigned int Image::width() const
{
	return (unsigned int) _size.cx;
}

unsigned int Image::height() const
{
	// The height may be stored as a negative number (top-down bitmaps).
	return (unsigned int) ( _size.cy > 0 ) ? _size.cy : -_size.cy;
}
